﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using mbine.msgc.tool;

namespace mbine.msgc.model
{
    public class CClient
    {
        public CCUser User { get; set; }
        public CRoster Roster { get; set; }
        public CDomain Domain { get; set; }
        public CServerMessage ServerMessage { get; set; }
        public Dictionary<string, string> Info { get; set; }

        public event EventHandler EventConnectionClose;
        public event EventHandler EventConnectionOpen;

        public event EventHandler EventLogin;
        public event EventHandler EventLogout;

        public CClient()
        {
            Domain = new CDomain();

            Init();
        }

        //Error
        void Instance_EventError(object sender, EventArgs e)
        {
            CLog.Instance.ELog(((CConnection)sender).ErrorMessage);
        }

        //연결 종료
        void Instance_EventClose(object sender, EventArgs e)
        {
            if (EventConnectionClose != null)
                EventConnectionClose(this, null);
        }

        //데이터 수신
        void Instance_EventDataReceive(object sender, EventArgs e)

        {
            string Header = ((CConnection)sender).Header;
            string SubHeader = ((CConnection)sender).SubHeader;
            string Body = ((CConnection)sender).Body;

            CLog.Instance.ILog(Header + SubHeader + Body);

            switch (Header.Substring(0, 12).Trim())
            {
                case "login":
                    if (EventLogin != null) EventLogin(this, null);
                    break;

                case "logout":
                    if (EventLogout != null) EventLogout(this, null);
                    break;

                case "roster":
                    CRoster.Instance.SetRoster(Body);
                    break;
            }
        }
        
        public void Close()
        {
            CConnection.Instance.Close();
        }

        public void Init()
        {
            //CLog 최초 생성
            CLog.Instance.LogPath = CInfo.Instance.Info["elogpath"];

            //CConnection 최초 생성
            CConnection.Instance.EventDataReceive += new EventHandler(Instance_EventDataReceive);
            CConnection.Instance.EventClose += new EventHandler(Instance_EventClose);
            CConnection.Instance.EventError += new EventHandler(Instance_EventError);
            CConnection.Instance.EventConnect += new EventHandler(Instance_EventConnect);

            CRoster.Instance.EventRosterChanged += new EventHandler(Instance_EventRosterChanged);
        }

        void Instance_EventRosterChanged(object sender, EventArgs e)
        {
            if (CRoster.Instance.IsLoginProc)
            {
                //로그인 시이기 때문에 Roster 화면만 변경한다.
            }
            else
            {
                //로그인 이후이기 때문에 모든 창들에 정보를 변경한다.
            }
        }

        //소켓 연결 완료
        void Instance_EventConnect(object sender, EventArgs e)
        {
            //최초 연결시에는 로그인 정보와 사용자 정보를 보낸다.
            CConnection.Instance.Send(CChatMessage.Login(CCUser.Instance.Email
                , CCUser.Instance.PrivateKey
                , CCUser.Instance.AuthKey
                , CRoster.Instance.GetRosterJson()));
        }
    }
}
